﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.MODEL.Custom_Model;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for AsSignEmployeeService
    /// </summary>
    public class AsSignEmployeeService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            var _model = new OTS_ASSIGN_PROJECT();
            if (request["oper"] == "add")
            {
                _model.EMPLOYEE_ID = request["EMP"];
                _model.PROJECT_ID = Convert.ToInt16(request["PROJECT_NAME"]);
                _model.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
                _model.STATUS = true;
                _model.CREATED = DateTime.Now;
                var cmd = new AsSignProject_Service(_model);
                cmd.Add();
            }
            else
            {
                if (request["oper"] == "del")
                {
                    _model.ID = Convert.ToInt16(request["ID"]);
                    var cmd = new AsSignProject_Service(_model);
                    cmd.Delete();
                }
                //else if (request["oper"] == "edit")
                //{
                //    _model.ID = Convert.ToInt16(request["ID"]);
                //    _model.NAME = request["PROJECT_NAME"];
                //    _model.SITE_ID = Convert.ToInt16(request["SITE_NAME"]);
                //    var cmd = new Project_Service(_model);
                //    cmd.Edit();
                //}
            }

            var _cmd = new AsSignProject_Service();
            var lst = new List<ASSIGN_PROJECT>();
            var dataSite = new Site_Service().GetAll();
            foreach (OTS_ASSIGN_PROJECT d in _cmd.GetAll())
            {
                var model = new ASSIGN_PROJECT();
                model.ID = d.ID;
                model.SITE_NAME = dataSite.Where(x => x.ID == d.OTS_PROJECT.SITE_ID).Select(y => y.NAME).FirstOrDefault();
                model.PROJECT_NAME = d.OTS_PROJECT.NAME;
                model.START_DATE = d.OTS_PROJECT.START_DATE;
                model.END_DATE = d.OTS_PROJECT.END_DATE;
                model.STATUS = d.OTS_PROJECT.STATUS;
                model.EMP = d.EMPLOYEE_ID + " : " + d.OTS_EMPLOYEE.NAME_TH;

                lst.Add(model);
            }
            lst = lst.OrderBy(o => o.ID).ToList();


            int total = lst.Count == 0 ? 1 : (int)Math.Ceiling((double)lst.Count / 10);

            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = lst.Count,
                rows = lst.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}